package com.zhq.blog.core.domain.admin;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;

import com.zhq.blog.core.domain.BasePersistentDomain;
import com.zhq.blog.core.domain.Context;


@Entity
public class AdminControllerCategory extends BasePersistentDomain<AdminControllerCategoryRepository> {
	
	private String name;
	
	@Transient private List<AdminControllerDescription> descriptions;
	
	public void init(){
		descriptions = context.getAdminControllerDescriptionRepository().findByCategoryId(this.getId());

        for (AdminControllerDescription each :descriptions){
            each.setCategory(this);
        }
	}
	
	public static AdminControllerDescription getByName(String name){
		List<AdminControllerCategory> categories = Context.current.getAdminControllerCategoryRepository().findAll();

        for (AdminControllerCategory each :categories){
            each.init();
        }
		for(AdminControllerCategory each : categories){
			for(AdminControllerDescription desc : each.getDescriptions()){
				if(desc.getName().equals(name)){
					return desc;
				}
			}
		}
		throw new RuntimeException("尚未定义这个Controller");
	}
	
	public AdminControllerCategory() {
		super();
	}
	
	public AdminControllerCategory(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<AdminControllerDescription> getDescriptions() {
		return descriptions;
	}

	public void setDescriptions(List<AdminControllerDescription> descriptions) {
		this.descriptions = descriptions;
	}
	
	
}
